package com.bgds.sys.api.project;


import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bgds.sys.api.bean.mars.dto.AlarmEventDto;
import com.bgds.sys.api.project.request.ProjectRequest;
import com.bgds.sys.api.project.service.ProjectService;
import com.bgds.sys.common.pagevo.Result;
import com.bgds.sys.common.utils.ResultUtil;
import com.bgds.sys.module.entity.project.Project;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;


/**
 * 项目管理
 *
 * @author chenchao
 * @date 2021/9/14
 */
@RestController
@RequestMapping("/project")
public class ProjectController {


    @Resource
    private ProjectService projectService;

    /**
     * 项目查询
     *
     * @param request
     * @return com.baomidou.mybatisplus.core.metadata.IPage<com.bgds.sys.module.entity.project.Project>
     * @author chenchao
     * @date 2021/9/14
     */

    @GetMapping
    public Result pageList(ProjectRequest request) {
        return new ResultUtil().setData(projectService.pageList(request));
    }


    /**
     * 功能描述
     *
     * @param id
     * @return com.bgds.sys.module.entity.project.Project
     * @author chenchao
     * @date 2021/9/14
     */
    @GetMapping("/{id}")
    public Result getProject(@PathVariable("id") Long id) {
        return new ResultUtil().setData(projectService.getProject(id));
    }

    /**
     * 添加
     *
     * @param project
     * @return void
     * @author chenchao
     * @date 2021/9/14
     */
    @PutMapping
    public Result addProject(@RequestBody Project project) {
        try {
            projectService.addProject(project);
        } catch (Exception e) {
            return new ResultUtil().setErrorMsg(e.getMessage());
        }
        return new ResultUtil().success();
    }


    /***
     * 修改
     * @author chenchao
     * @date 2021/9/14
     * @param project
     * @return void
     */
    @PostMapping
    public Result saveProject(@RequestBody Project project) {
        projectService.saveProject(project);
        return new ResultUtil().success();
    }

    @DeleteMapping("/{id}")
    public Result deleteProject(@PathVariable("id") Long id) {
        projectService.deleteProject(id);
        return new ResultUtil().success();
    }

    /**
     * 导出
     *
     * @param ids
     * @return void
     * @author chenchao
     * @date 2021/9/15
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response, String ids) {
        projectService.export(response, ids);
    }

    @PostMapping("/import")
    public Result Import(@RequestParam("file") MultipartFile file) {
        projectService.Import(file);
        return new ResultUtil().setSuccessMsg("导入成功!");
    }
}
